﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace KrileUpdater
{
    public partial class Behind : Form
    {
        public Behind()
        {
            InitializeComponent();
        }


        private Process RunAsStart()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Application.ExecutablePath;
            var cmd = Environment.GetCommandLineArgs();
            if (cmd.Length < 2)
            {
#if DEBUG
                cmd = new[] { "", "0.00" };
#else
                MessageBox.Show("Information losted.", "Updater", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                Application.Exit();
                return null;
#endif
            }
            double ver = double.Parse(cmd[1]);
            int pid;
            if (cmd.Length >= 3 && int.TryParse(cmd[2], out pid))
            {
                startInfo.Arguments = ver.ToString() + " " + pid.ToString() + " runas";
            }
            else
            {
                startInfo.Arguments = ver.ToString() + " runas";
            }
            if (Environment.OSVersion.Version.Major >= 6)
            {
                startInfo.Verb = "runas";
            }
            try
            {
                return Process.Start(startInfo);
            }
            catch
            {
                return null;
            }
        }

        private void Behind_Shown(object sender, EventArgs e)
        {
            var cmd = Environment.GetCommandLineArgs();
#if !DEBUG
            if (cmd.Length < 2)
            {
                MessageBox.Show("Information losted.", "Updater", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                Application.Exit();
                return;
            }
#endif
            var p = RunAsStart();
            while(p == null)
            {
                if (MessageBox.Show(
                    "Fail to start updater with elevated authority." + Environment.NewLine +
                    "Do you want to retry?",
                    "Krile updater - Execution error",
                     MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                    break;
                p = RunAsStart();
            }
            if (p == null)
            {
                Application.Exit();
            }
            p.EnableRaisingEvents = true;
            p.SynchronizingObject = this;
            p.Exited += new EventHandler(p_Exited);
        }

        void p_Exited(object sender, EventArgs e)
        {
            var up = new UpdateProcessor(null);
            System.Diagnostics.Process.Start(Path.Combine(Application.StartupPath, up.GetCallbackFile()));
            Application.Exit();
        }
    }
}
